package cn.com.callback

import cn.com.elite.EliteBean
import cn.com.kh.KHDBConfig
import cn.com.kh.KHUtil
import cn.com.kh.LogAnalyticJSSTrail
import cn.com.kh.apiResult
import com.alibaba.fastjson.JSONObject
import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory

import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

/**
 * @projectName: Groovy
 * @package: com.elite.groovy.callback
 * @className: apiChatClientLogin
 * @description: 网聊登录用户身份鉴权及队列识别
 * @author: LiJT
 * @date: 2023-10-27 11:13
 * @version: 1.0
 */

class apiLogAnalysis implements ServiceBackInvoke {
    private static final Log log = LogFactory.getLog(apiLogAnalysis.class);

    String doCallBack(EliteBean eliteBean, HttpServletRequest request, HttpServletResponse response) {

        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json;charset=UTF-8");
        long startTime = System.currentTimeMillis();
        String outData = "";

        // 调用IP过滤
        if (KHDBConfig.getParam(eliteBean, "#APIIP") == "1" && !KHUtil.getRemoteHost(request)) {
            outData = "API被非法IP调用-Request Not Allow!" + KHUtil.getRemoteHost(request);
            log.warn(outData);
            return JSONObject.toJSONString(new apiResult(apiResult.ERROR, outData));
        }

        String getMethod = request.getMethod();
        log.debug("HTTP请求调用类型 getMethod:" + getMethod);
        if (getMethod != "GET") {
            outData = "HTTP请求调用类型错误，接口调用失败：" + getMethod;
            log.error(outData);
            return JSONObject.toJSONString(new apiResult(apiResult.ERROR, outData));
        }

        // 获取所有Header
        Map<String, String> mapHeader = new HashMap<String, String>();
        Enumeration headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String key = (String) headerNames.nextElement();
            String value = request.getHeader(key);
            log.debug("Call Header-" + key + ":" + value);

            mapHeader.put(key, value);
        }

        // 获取所有参数名
        Map<String, String> mapParam = new HashMap<String, String>();
        Enumeration ep = request.getParameterNames();
        while (ep.hasMoreElements()) {
            String key = (String) ep.nextElement();
            String value = request.getParameter(key);
            log.debug("Call Parameter-" + key + ":" + value);
            mapParam.put(key, value);

        }

        // 获取嵌入传递的参数
        String log_path = mapParam.get("log_path");
        if (KHUtil.isNull(log_path)) {
            outData = "HTTP请求调用错误 log_path：" + log_path;
            log.error(outData);
            return JSONObject.toJSONString(new apiResult(apiResult.ERROR, outData, outData));
        }

        try {
            // 調用
            LogAnalyticJSSTrail.doMain(eliteBean, log_path);
            outData = JSONObject.toJSONString(new apiResult(apiResult.SUCCESS, "处理完成：" + outData + " 总耗时:" + (System.currentTimeMillis() - startTime).toString(), (System.currentTimeMillis() - startTime)));
            return outData;

        } catch (Exception e) {
            outData = "HTTP请求调用错误，接口处理异常：" + e.getMessage();
            log.error(outData, e);
            return JSONObject.toJSONString(new apiResult(apiResult.ERROR, outData, e.getMessage()));

        }
    }

}